﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMusicStore.Models;

namespace MvcMusicStore.Controllers
{
    public class HomeController : Controller
    {
        // GET: /Home/
        private MusicShopDBEntities model = new MusicShopDBEntities();
    
        public ActionResult Index()
        {
            
            return View();
        }
        [ChildActionOnly]
        public ActionResult TopSellingAlbums()
        {
            var albums = GetTopSellingAlbums(5);
            return PartialView(albums);
        }

        [ChildActionOnly]
        public ActionResult TopNewAlbums()
        {
            var albums = GetTopNewAlbums(5);
            return PartialView(albums);
        }
        [ChildActionOnly]
        public ActionResult TopSellingSongs()
        {
            var songs = GetTopSellingSong(5);
            return PartialView(songs);
        }
        [ChildActionOnly]
        public ActionResult TopNewSongs()
        {
            var songs = GetTopNewSong(5);
            return PartialView(songs);
        }

        private List<Album> GetTopSellingAlbums(int count)
        {
            // Group the order details by album and return
            // the albums with the highest count
            return model.Album
                .OrderByDescending(a => a.counterBought)
                .Take(count)
                .ToList();
        }
        private List<Album> GetTopNewAlbums(int count)
        {
            
            return model.Album
                .OrderByDescending(a => a.dateAdd)
                .Take(count)
                .ToList();
        }
        private List<Song> GetTopNewSong(int count)
        {
            
            return model.Song
                .OrderByDescending(a => a.dateAdd)
                .Take(count)
                .ToList();
        }
        private List<Song> GetTopSellingSong(int count)
        {
            
            return model.Song
                .OrderByDescending(a => a.counterBought)
                .Take(count)
                .ToList();
        }
        protected override void Dispose(bool disposing)
        {

            model.Dispose();
            base.Dispose(disposing);

        }
    }
}
      
 